﻿Public Class AgoresControl

    Private Sub makeDataGrid()
        DataGridView1.Columns.Add("proion", "Προϊόν")
        DataGridView1.Columns.Add("onomaP", "Όνομα Προϊόντος")
        DataGridView1.Columns.Add("quant", "Ποσότητα")
        DataGridView1.Columns.Add("price", "Τιμή Μονάδας")
        DataGridView1.Columns.Add("pricesinolo", "Συνολική Τιμή")
    End Sub
    Private Function keepetaira() As String
        Dim fileReader As String = "none"
        Try
            fileReader = My.Computer.FileSystem.ReadAllText(Application.StartupPath & _
                                            "\keppetairia.txt")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return fileReader
    End Function
    Private Sub AgoresControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim PLadapter As hard_oneDataSetTableAdapters.proiontaTableAdapter = New hard_oneDataSetTableAdapters.proiontaTableAdapter
        Dim PLtable As hard_oneDataSet.proiontaDataTable = New hard_oneDataSet.proiontaDataTable

        PLadapter.FillBy(PLtable, keepetaira())
        ProiontaComboBox.DataSource = PLtable
        ProiontaComboBox.DisplayMember = "idProionta"
        ProiontaComboBox.ValueMember = "idProionta"

        makeDataGrid()

    End Sub
    Private Function SaveChanges() As Boolean
        If ValidateData() Then
            Me.TableAdapterManager.UpdateAll(Me.Hard_oneDataSet)
            Return True
        End If
        Return False
    End Function

    Protected Overloads Overrides Sub OnCreateControl()
        MyBase.OnCreateControl()
        AddHandler Me.ParentForm.FormClosing, AddressOf ParentForm_FormClosing
    End Sub
    Private Function ValidateData() As Boolean
        If Me.Validate() Then
            Try
                AgoresBindingSource.EndEdit()
                Return True
            Catch ex As Exception
                If MessageBox.Show( _
                  "Invalid data was entered. Cancel edits?", _
                  "Agores", MessageBoxButtons.YesNo, _
                  MessageBoxIcon.Error) = DialogResult.Yes Then

                    AgoresBindingSource.CancelEdit()
                End If
                Return False
            End Try
        End If
        Return False
    End Function
    Private Sub ParentForm_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
        If ValidateData() Then
            If Me.Hard_oneDataSet.agores.GetChanges _
            IsNot Nothing Then
                Dim response As Integer = _
                MessageBox.Show( _
                "Do you want to save your changes?", _
                "Changes have been made", _
                MessageBoxButtons.YesNoCancel, _
                MessageBoxIcon.Warning)
                If response = DialogResult.Yes Then
                    If SaveChanges() Then
                        MessageBox.Show("Your changes were saved", _
                        "Agores", MessageBoxButtons.OK)
                    Else
                        e.Cancel = True
                    End If
                ElseIf response = DialogResult.Cancel Then
                    e.Cancel = True
                End If
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim counter = Convert.ToInt32(DataGridView1.Rows.Count.ToString)
        Me.AgoresTableAdapter.InsertQuery(IdAgorasTextBox.Text, ImerominiaDateTimePicker.Value.Date, Emporiki_katigoriaTextBox.Text, Convert.ToDouble(Sinoliki_timiTextBox.Text), Promitheytes_idPromitheytiTextBox.Text)
        For i As Integer = 0 To counter - 1
            Dim adapter As New hard_oneDataSetTableAdapters.proionta_has_agoresTableAdapter
            Dim table As New hard_oneDataSet.proiontaDataTable

            'MessageBox.Show(DataGridView1.Rows(i).Cells("proion").Value.ToString & "  " & IdAgorasTextBox.Text & "   " & DataGridView1.Rows(i).Cells("quant").Value)
            adapter.InsertProiontaAgoras(DataGridView1.Rows(i).Cells("proion").Value.ToString, IdAgorasTextBox.Text, DataGridView1.Rows(i).Cells("quant").Value)

        Next (i)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        DataGridView1.Rows.Add(New String() {ProiontaComboBox.SelectedValue.ToString, onomaProion.Text, PosotitaTextBox.Text, priceMonadas.Text, (Convert.ToDouble(priceMonadas.Text) * Convert.ToInt32(PosotitaTextBox.Text))})
        Dim counter = Convert.ToInt32(DataGridView1.Rows.Count.ToString)
        Dim temp = 0.0
        For i As Integer = 0 To counter - 1
            temp += Convert.ToDouble(DataGridView1.Rows(i).Cells("pricesinolo").Value.ToString)
        Next (i)
        Sinoliki_timiTextBox.Text = temp.ToString
    End Sub

    Private Sub ProiontaComboBox_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProiontaComboBox.Leave
        Dim adapter As New hard_oneDataSetTableAdapters.proiontaTableAdapter
        Dim table As New hard_oneDataSet.proiontaDataTable
        adapter.FillByProion(table, ProiontaComboBox.SelectedValue.ToString)

        priceMonadas.Text = table.Rows(0).Item("timi_xondrikis").ToString()
        onomaProion.Text = table.Rows(0).Item("perigrafi").ToString()
    End Sub
 
End Class
